package com.ghost;

import java.util.LinkedList;

import org.loon.framework.android.game.action.sprite.ISprite;
import org.loon.framework.android.game.action.sprite.Label;
import org.loon.framework.android.game.action.sprite.Picture;
import org.loon.framework.android.game.action.sprite.Sprite;
import org.loon.framework.android.game.action.sprite.Sprites;
import org.loon.framework.android.game.action.sprite.StatusBar;
import org.loon.framework.android.game.core.graphics.LColor;
import org.loon.framework.android.game.core.graphics.LFont;
import org.loon.framework.android.game.core.graphics.LImage;
import org.loon.framework.android.game.core.graphics.Screen;
import org.loon.framework.android.game.core.graphics.Trans;
import org.loon.framework.android.game.core.graphics.device.LGraphics;
import org.loon.framework.android.game.core.graphics.window.LMessage;
import org.loon.framework.android.game.core.graphics.window.LPaper;
import org.loon.framework.android.game.core.graphics.window.LSelect;
import org.loon.framework.android.game.core.timer.LTimer;
import org.loon.framework.android.game.core.timer.LTimerContext;

import com.ghost.Chessboard.Chessboard;
import com.ghost.Chessboard.Item;

import android.content.res.Resources;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import com.ghost.*;

public class Fortscreen extends Screen {

    private String SORRY;
    private String START_MES, SORRY1_MES, SORRY2_MES, SORRY3_MES, EASY_MES;

    private String WAIT_MES;
    private String HELP_MES;
    private String[] HELP_MESSAGE_LIST;

    private int bomb_number, refresh_number, tip_number, progress_number;

    private int xBound;

    private int yBound;

    private int pcount;

    private int refreshcount;

    private int bombcount;

    private int tipcount;

    private int sub;

    private LTimer timer, timer1;

    private Level levelInfo;

    private Grid grid[][];
    private Grid gridcards[];

    private Grid nexts;

    private Grid nexte;

    private LinkedList<Grid>[] path;

    private Thread clickThread;

    private StatusBar progress;

    private Label stage, time;

    private Picture role;

    private LPaper title, over;

    private Grid prev;

    private LMessage mes;

    private LSelect select;

    private Sprite helpRole;

    private boolean wingame, failgame, init, overFlag;

    private int stageNo, count, delCount;

    private int offsetX, offsetY;
    private int listchessboard[][];
    private Chessboard c1;
    public boolean isLandscape = true;

    public Fortscreen() {

    }

    public Fortscreen(Resources res) {

	SORRY = res.getString(R.string.sorry);
	START_MES = res.getString(R.string.start_mes);
	SORRY1_MES = SORRY + res.getString(R.string.sorry1_mes);
	SORRY2_MES = SORRY + res.getString(R.string.sorry2_mes);
	SORRY3_MES = SORRY + res.getString(R.string.sorry3_mes);
	EASY_MES = res.getString(R.string.easy_mes);
	WAIT_MES = res.getString(R.string.wait_mes);
	HELP_MES = res.getString(R.string.help_mes);
	HELP_MESSAGE_LIST = res.getStringArray(R.array.help_message_list);

    }

    public void onLoad() {
	setBackground(Images.getInstance().getImage(10));
	stage(1);
    }

    @SuppressWarnings("unchecked")
    private void reset() {
	overFlag = false;
	failgame = false;
	if (path == null) {
	    path = new LinkedList[3];
	}
	path[0] = new LinkedList<Grid>();
	path[1] = new LinkedList<Grid>();
	path[2] = new LinkedList<Grid>();
	init = false;
	count = 0;
	if (progress != null) {
	    progress.set(progress_number);
	}
	initUI();
	System.out.println("###$$$$$$$:isLandscape  2"
		+ String.valueOf(isLandscape));
	if (!isLandscape) screenChanged(isLandscape);

    }

    /**
     * 选择游戏关卡
     * 
     * @param no
     */
    private void stage(int no) {
	switch (no) {
	case 1:
	    stageNo = 1;
	    sub = 6;
	    bomb_number = 3;
	    refresh_number = 3;
	    tip_number = 3;
	    progress_number = 99999;
	    levelInfo = new Level(5, 5);
	    break;
	case 2:
	    stageNo = 2;
	    sub = 6;
	    bomb_number = 2;
	    refresh_number = 2;
	    tip_number = 2;
	    progress_number = 6400;
	    levelInfo = new Level(6, 4);
	    break;
	case 3:
	    stageNo = 3;
	    sub = 6;
	    bomb_number = 1;
	    refresh_number = 1;
	    tip_number = 1;
	    progress_number = 5400;
	    levelInfo = new Level(6, 4);
	    break;
	case 4:
	    stageNo = 4;
	    sub = 6;
	    bomb_number = 1;
	    refresh_number = 1;
	    tip_number = 1;
	    progress_number = 10800;
	    levelInfo = new Level(6, 5);
	    break;
	case 5:
	    stageNo = 5;
	    sub = 8;
	    bomb_number = 1;
	    refresh_number = 1;
	    tip_number = 1;
	    progress_number = 12400;
	    levelInfo = new Level(8, 5);
	    break;
	default:
	    stageNo++;
	    sub = 20;
	    bomb_number = 1;
	    refresh_number = 1;
	    tip_number = 1;
	    progress_number = 19400;
	    levelInfo = new Level(8, 5);
	    break;
	}
	reset();
    }

    public void dispose() {

    }

    private void initRole() {

	delCount = 0;
	role = new Picture(Images.getInstance().getImage(11));
	helpRole = new Sprite(Images.getInstance().getImage(8));
	helpRole.setLocation(Fortscreen.this.getWidth()- helpRole.getWidth() - 10,
		Fortscreen.this.getHeight()- helpRole.getHeight()- 10);
	helpRole.setVisible(false);
	//Fortscreen.this.add(helpRole);
	mes = new LMessage(Images.getInstance().getImage(14),
		(getWidth() - 460) / 2, getHeight() - 126 - 10) {
	    public void doClick() {
		if (!init) {
		    if (count == 0) {
			role.setImage(Images.getInstance().getImage(12));
			setMessage(START_MES);
		    } else if (isComplete()) {
			Runnable runnable = new Runnable() {
			    public void run() {

				stage = new Label("Stage - " + stageNo, 160, 25);
				stage.setColor(LColor.black);
				stage.setFont(LFont.getFont("Dialog", 1, 20));
				Fortscreen.this.add(stage);
				time = new Label("Time", 270, 25);
				time.setColor(LColor.black);
				time.setFont(LFont.getFont("Dialog", 1, 20));
				Fortscreen.this.add(time);
				setVisible(false);
				role.setVisible(false);
				setVisible(false);
				init = true;
				count = 0;

				progress = new StatusBar(progress_number,
					progress_number, 325, 5, 150, 25);
				progress.setDead(true);
				Fortscreen.this.add(progress);
				if (title == null) {
				    title = new LPaper(Images.getInstance()
					    .getImage(15), 55, 55);
				} else {
				    title.setLocation(55, 55);
				}
				centerOn(title);
				Fortscreen.this.add(title);
				if (true) {
				    if (helpRole == null) {
//					helpRole = new Sprite(Images
//						.getInstance().getImage(8));
//					helpRole.setLocation(Fortscreen.this
//						.getWidth()
//						- helpRole.getWidth() - 10,
//						Fortscreen.this.getHeight()
//							- helpRole.getHeight()
//							- 10);
//					Fortscreen.this.add(helpRole);
				    } else {
					helpRole.setVisible(true);
					Fortscreen.this.add(helpRole);
				    }
				}
//				else {
//				    if (helpRole != null) {
//					helpRole.setVisible(false);
//				    }
//				}

			    }
			};
			callEvent(runnable);

		    }
		    count++;
		}

		if (HELP_MES.equalsIgnoreCase(getMessage()) && isComplete()) {
		    setVisible(false);
		    select = new LSelect(Images.getInstance().getImage(14),
			    (Fortscreen.this.getWidth() - 460) / 2,
			    Fortscreen.this.getHeight() - 126 - 10) {
			public void doClick() {

			    switch (getResultIndex()) {
			    case 0:
				mes.setVisible(true);
				if (refreshcount > 0) {
				    mes.setMessage(EASY_MES);
				    Fortscreen.this.refreshs();
				} else {
				    mes.setMessage(SORRY1_MES);
				}
				Fortscreen.this.remove(this);
				break;
			    case 1:
				mes.setVisible(true);
				if (tipcount > 0) {
				    mes.setMessage(EASY_MES);
				    Fortscreen.this.showNext();
				} else {
				    mes.setMessage(SORRY2_MES);
				}
				Fortscreen.this.remove(this);
				break;
			    case 2:
				mes.setVisible(true);
				if (bombcount > 0) {
				    mes.setMessage(EASY_MES);
				    Fortscreen.this.useBomb();
				} else {
				    mes.setMessage(SORRY3_MES);

				}
				Fortscreen.this.remove(this);
				break;
			    case 3:
				mes.setVisible(true);
				Fortscreen.this.remove(this);
				mes.setVisible(false);
				role.setVisible(false);
				helpRole.setVisible(true);
				if (stage != null) {
				    stage.setVisible(true);
				}
				break;
			    default:
				break;
			    }
			}

		    };
		    select.setFontColor(LColor.black);
		    select.setAlpha(0.8f);
		    select.setTopOffset(-5);
		    select.setMessage(HELP_MESSAGE_LIST);
		    Fortscreen.this.add(select);
		    return;

		} else if ((EASY_MES.equalsIgnoreCase(getMessage()) || getMessage()
			.startsWith(SORRY))
			&& isComplete()) {

		    mes.setVisible(false);
		    role.setVisible(false);
		    helpRole.setVisible(true);
		    if (stage != null) {
			stage.setVisible(true);
		    }
		}
	    }
	};
	mes.setMessageLength(20);
	mes.setAlpha(0.8f);
	mes.setFontColor(LColor.black);
	mes.setMessage(WAIT_MES);
	add(role);
	add(mes);

    }

    public void alter(LTimerContext t) {
	if (isWait()) {
	    return;
	}
	if (timer1 == null) {
	    timer1 = new LTimer(50);
	}
	if (title != null && timer1.action(t.getTimeSinceLastUpdate())) {
	    if (title.getY() > 50) {
		title.move_up(8);
		title.validatePosition();
	    } else if (title.getAlpha() > 0.2f) {
		title.setAlpha(title.getAlpha() - 0.1f);
	    } else {
		title.setVisible(false);
		remove(title);
		title = null;
	    }
	    return;
	} else if (over != null && timer1.action(t.getTimeSinceLastUpdate())
		&& !overFlag) {
	    if (over.getY() < (getHeight() - over.getHeight()) / 2) {
		over.move_down(8);
		over.validatePosition();
	    } else if (over.getAlpha() < 1.0f) {
		over.setAlpha(over.getAlpha() + 0.1f);
	    } else {
		centerOn(over);
		overFlag = true;
	    }

	    return;
	}
	if (!wingame) {
	    if (timer == null) {
		timer = new LTimer(100);
	    }
	    if (timer.action(t.getTimeSinceLastUpdate())) {
		if (progress != null) {

		    progress.setUpdate(progress.getValue() - (stageNo * 10));
		    if (progress.getValue() <= 100 && !failgame) {

			failgame = true;
			getSprites().setVisible(false);

			over = new LPaper(Images.getInstance().getImage(16), 0,
				0) {
			    public void doClick() {
				if (getAlpha() >= 1.0 && overFlag) {
				    over = null;
				    removeAll();
				    stage(stageNo);
				    getSprites().setVisible(true);
				}
			    }
			};
			over.setAlpha(0.1f);
			centerOn(over);
			over.setY(0);
			add(over);
		    }
		}

	    }

	} else {
	    wingame = false;
	    removeAll();
	    stage(stageNo + 1);
	}
    }

    private void initUI() {

	// xBound = levelInfo.getXBound() + 2;
	// yBound = levelInfo.getYBound() + 2;
	xBound = levelInfo.getXBound();
	yBound = levelInfo.getYBound();

	grid = new Grid[yBound][xBound];

	// the cards of monster,index of values 3,4,5 of images
	gridcards = new Grid[3];
	for (int i = 0; i < gridcards.length; i++) {

	    LImage img1 = Images.getInstance().getImage(i + 3);
	    gridcards[i] = new Grid(i, 0);
	    gridcards[i].setImage(img1);
	    gridcards[i].setBorder(4);
	    gridcards[i].itemnumber = i + 3;
	    gridcards[i].canbetouch = true;
	    offsetX = 10; // 设置开始的xy
	    offsetY = 10; //
	    int nxc = offsetX + 0 * img1.getWidth();
	    int nyc = offsetY + (i + 1) * img1.getHeight();

	    // //判断开始时候是否需要旋转
	    // if(isLandscape==false)
	    // gridcards[i].rotateimg(isLandscape);

	    gridcards[i].setLocation(nxc, nyc);
	    getSprites().add(gridcards[i]);

	}

	int[] layout = { 0, 1, 0, 1, 0, 2, 0, 2, 0, 1, 1, 1, 0, 2, 0, 0, 2, 0,
		2, 0, 1, 0, 1, 0, 2 }; // 布局
	int[] resultelist = { 4, 0, 2, 2, 3, 3, 1, 0, 4, 0, 1, 0, 0, 1, 2, 2,
		1, 3, 2, 1 };

	int count = 0;

	Grid temp[] = new Grid[xBound * yBound];
	c1 = new Chessboard(xBound, yBound, layout, resultelist);
	for (int y = 0; y < yBound; y++) {
	    for (int x = 0; x < xBound; x++) {
		grid[y][x] = new Grid(x, y);

		int imgnumber;
		switch (layout[count]) {
		case 0: // blank
		    imgnumber = 0;
		    break;
		case 1: // mirror_left
		    imgnumber = 1;
		    break;
		case 2: // mirror_right
		    imgnumber = 2;
		    break;
		default:
		    imgnumber = 0;
		    break;
		}
		LImage img = Images.getInstance().getImage(imgnumber);
		grid[y][x].setImage(img);
		grid[y][x].setBorder(3);
		grid[y][x].itemnumber = imgnumber;
		if (imgnumber == 0)
		    grid[y][x].canbereplace = true;
		grid[y][x].resize(32, 32); // 缩放 待加判断缩放w,h
		offsetX = 100; // 设置棋盘开始的xy
		offsetY = 50; //
		int nx = offsetX + x * grid[y][x].getImage().getWidth();
		int ny = offsetY + y * grid[y][x].getImage().getHeight();

		grid[y][x].setLocation(nx, ny);
		temp[count] = grid[y][x];
		count++;
		getSprites().add(grid[y][x]);
	    }

	}

	// shuffle(temp, count);
	wingame = false;
	tipcount = tip_number;
	bombcount = bomb_number;
	refreshcount = refresh_number;
	initRole();
    }

    public void setPaused(boolean p) {
	if (p) {
	    getSprites().setVisible(false);
	} else {
	    getSprites().setVisible(true);
	}
    }

    public boolean isWait() {
	boolean result = false;
	if (role != null) {
	    result = role.isVisible();
	}
	return result;
    }

    public void refreshs() {
	if (wingame || progress.getValue() == 0) {
	    return;
	}
	if (progress != null) {
	    progress.set(progress_number);
	}
	wingame = false;
	overFlag = false;
	failgame = false;
	init = false;
	Grid temp[] = new Grid[xBound * yBound];
	int count = 0;
	for (int y = 1; y < yBound - 1; y++) {
	    for (int x = 1; x < xBound - 1; x++)
		if (grid[y][x].isVisible()) {
		    int nx = offsetX + x * grid[y][x].getWidth();
		    int ny = offsetY + y * grid[y][x].getHeight();
		    grid[y][x].setLocation(nx, ny);
		    grid[y][x].setBorder(3);
		    temp[count] = grid[y][x];
		    count++;
		}

	}
	if (count != 0) {
	    refreshcount--;
	    // shuffle(temp, count);
	} else {
	    wingame = true;
	}
    }

    private void deletePair(Grid prev, Grid current) {
	// LinkedList<Grid> temp = new LinkedList<Grid>();
	// temp.add(prev);
	// for (int i = 0; i < pcount; i++) {
	// temp.addAll(path[i]);
	// path[i].clear();
	// }
	//
	// AnimateThread thread = new AnimateThread(temp);
	// thread.start();

    }

    public void showNext() {
	if (wingame || progress.getValue() == 0 || tipcount == 0) {
	    return;
	}
	tipcount--;
	if (nexts != null && nexte != null) {
	    nexts.setBorder(2);
	    nexte.setBorder(2);
	}
    }

    public void useBomb() {
	if (wingame || progress.getValue() == 0 || bombcount == 0) {
	    return;
	}
	bombcount--;
	if (nexts != null && nexte != null) {
	    deletePair(nexts, nexte);
	}
    }

    // 纯组件制作，所以不需要手动绘图。

    public void draw(LGraphics g) {

    }

    private Grid getGrid(int x, int y) {
	Sprites ss = getSprites();
	if (ss == null) {
	    return null;
	}
	ISprite[] s = ss.getSprites();
	for (int i = 0; i < s.length; i++) {
	    if (s[i] instanceof Grid) {
		Grid g = (Grid) s[i];
		if (g.getCollisionBox().contains(x, y)) {
		    return g;
		}
	    }
	}
	return null;
    }

    class Level {
	private int xBound;
	private int yBound;

	public Level() {
	    xBound = 8;
	    yBound = 6;
	}

	public Level(int x, int y) {
	    xBound = x;
	    yBound = y;
	}

	public int getXBound() {
	    return xBound;
	}

	public int getYBound() {
	    return yBound;
	}
    }

    public void onTouch(float x, float y, MotionEvent e, int pointerCount,
	    int pointerId) {

    }

    public boolean onKeyDown(int keyCode, KeyEvent e) {
	return false;
    }

    public boolean onKeyUp(int keyCode, KeyEvent e) {
	return false;
    }

    public boolean onTouchDown(MotionEvent e) {
	if (!init) {
	    return false;
	}
	if (failgame) {
	    return false;
	}
	if (wingame || progress.getValue() == 0) {
	    return false;
	}
	// if (nexte != null && nexts != null) {
	if (helpRole != null) {
	    if (!role.isVisible() && helpRole.isVisible()) {
		if (failgame) {
		    return false;
		}
		if (onClick(helpRole)) {
		    if (stage != null) {
			stage.setVisible(false);
		    }
		    helpRole.setVisible(false);
		    role.setImage(Images.getInstance().getImage(13));
		    role.setVisible(true);
		    mes.setMessageLength(20);
		    mes.setMessage(HELP_MES);
		    mes.setVisible(true);
		    return true;
		}
	    }
	}
	// }

	if (clickThread != null) {
	    clickThread = null;
	    return false;
	}

	clickThread = new Thread() {
	    public void run() {
		try {
		    if (getGrid(getTouchX(), getTouchY()) == prev) {
			return;
		    }
		    if (prev == null) {
			if (getGrid(getTouchX(), getTouchY()).canbetouch) {
			    prev = getGrid(getTouchX(), getTouchY());
			    if (prev != null) {
				prev.setBorder(0);
			    }
			}
		    } else {
			if (progress.getValue() == 0) {
			    return;
			}
			if (getGrid(getTouchX(), getTouchY()).canbetouch) {
			    prev.setBorder(3);
			    prev = getGrid(getTouchX(), getTouchY());
			    if (prev != null) {
				prev.setBorder(0);
			    }
			} else {
			    if (prev != null
				    && getGrid(getTouchX(), getTouchY()).canbereplace) {

				// getGrid(getTouchX(),
				// getTouchY()).setImage(prev.getImage());
				getGrid(getTouchX(), getTouchY())
					.setImage_lastsize(prev.getImage());// 设置新img，尺寸按旧尺寸缩放
				getGrid(getTouchX(), getTouchY()).itemnumber = prev.itemnumber;
				int nowx = getGrid(getTouchX(), getTouchY())
					.getXpos();
				int nowy = getGrid(getTouchX(), getTouchY())
					.getYpos();
				c1.placeitem(new Item(getGrid(getTouchX(),
					getTouchY()).itemnumber), nowx, nowy);
				c1.check();
				String theinfo = "";
				String thesee = "";
				for (int i = 0; i < c1.nowlayout.length; i++) {
				    theinfo += String.valueOf(c1.nowlayout[i]);
				}
				Log.v("#########layout", theinfo);

				for (int i = 0; i < c1.getseelist().length; i++) {
				    thesee += String
					    .valueOf(c1.getseelist()[i]);
				}
				Log.v("#########thesee", thesee);

				prev.setBorder(3);
				prev = null;
				// 给空格填怪物图片
				// 下一步加棋盘数组处理：待加
			    } else {
				prev.setBorder(0);
			    }
			}

			if (prev == null) {
			    return;
			}

		    }
		} catch (Exception ex) {

		}
	    }
	};
	clickThread.start();
	clickThread = null;
	return true;
    }

    public boolean onTouchMove(MotionEvent e) {

	return true;
    }

    public boolean onTouchUp(MotionEvent e) {
	return true;
    }

    public void screenChanged(boolean nowisLandscape) {
	this.isLandscape = nowisLandscape;
	if (gridcards != null)
	    for (int i = 0; i < gridcards.length; i++) {
		gridcards[i].rotateimg(nowisLandscape);

	    }
	if (helpRole != null) {
	    int x = (nowisLandscape ? Trans.TRANS_NONE :Trans.TRANS_ROT270  );
	    helpRole.setTransform(x);

	}

    }

//    public void startrotate(boolean nowisLandscape) {
//	if (gridcards != null && nowisLandscape == false) {
//
//	    for (int i = 0; i < gridcards.length; i++) {
//		gridcards[i].rotateimg(false);
//	    }
//	}
//	if (helpRole != null) {
//	     int x = (nowisLandscape ? Trans.TRANS_NONE :Trans.TRANS_ROT270);
//	     helpRole.setTransform(x);
//
//	}
//
//    }
    


    // private boolean findPair() {
    // nexts = null;
    // nexte = null;
    // for (int sy = 1; sy < yBound - 1; sy++) {
    // for (int sx = 1; sx < xBound - 1; sx++)
    // if (grid[sy][sx].isVisible()) {
    // for (int ey = sy; ey < yBound - 1; ey++) {
    // for (int ex = 1; ex < xBound - 1; ex++)
    // if (grid[ey][ex].isVisible()
    // && (ey != sy || ex != sx)
    // && grid[sy][sx].equals(grid[ey][ex])) {
    // pcount = findPath(grid[sy][sx], grid[ey][ex]);
    // if (pcount != 0) {
    // nexts = grid[sy][sx];
    // nexte = grid[ey][ex];
    // return true;
    // }
    // }
    //
    // }
    //
    // }
    //
    // }
    //
    // return false;
    // }

    // private boolean xdirect(Grid start, Grid end, LinkedList<Grid> path) {
    // if (start.getYpos() != end.getYpos())
    // return false;
    // int direct = 1;
    // if (start.getXpos() > end.getXpos()) {
    // direct = -1;
    // }
    // path.clear();
    // for (int x = start.getXpos() + direct; x != end.getXpos() && x < xBound
    // && x >= 0; x += direct) {
    // if (grid[start.getYpos()][x].isVisible()) {
    // return false;
    // }
    // path.add(grid[start.getYpos()][x]);
    // }
    //
    // path.add(end);
    // return true;
    // }
    //
    // private boolean ydirect(Grid start, Grid end, LinkedList<Grid> path) {
    // if (start.getXpos() != end.getXpos()) {
    // return false;
    // }
    // int direct = 1;
    // if (start.getYpos() > end.getYpos()) {
    // direct = -1;
    // }
    // path.clear();
    // for (int y = start.getYpos() + direct; y != end.getYpos() && y < yBound
    // && y >= 0; y += direct) {
    // if (grid[y][start.getXpos()].isVisible()) {
    // return false;
    // }
    // path.add(grid[y][start.getXpos()]);
    // }
    //
    // path.add(end);
    // return true;
    // }

    // private int findPath(Grid start, Grid end) {
    // if (xdirect(start, end, path[0])) {
    // return 1;
    // }
    // if (ydirect(start, end, path[0])) {
    // return 1;
    // }
    // Grid xy = grid[start.getYpos()][end.getXpos()];
    // if (!xy.isVisible() && xdirect(start, xy, path[0])
    // && ydirect(xy, end, path[1])) {
    // return 2;
    // }
    // Grid yx = grid[end.getYpos()][start.getXpos()];
    // if (!yx.isVisible() && ydirect(start, yx, path[0])
    // && xdirect(yx, end, path[1])) {
    // return 2;
    // }
    // path[0].clear();
    // for (int y = start.getYpos() - 1; y >= 0; y--) {
    // xy = grid[y][start.getXpos()];
    // yx = grid[y][end.getXpos()];
    // if (xy.isVisible()) {
    // break;
    // }
    // path[0].add(xy);
    // if (!yx.isVisible() && xdirect(xy, yx, path[1])
    // && ydirect(yx, end, path[2])) {
    // return 3;
    // }
    // }
    //
    // path[0].clear();
    // for (int y = start.getYpos() + 1; y < yBound; y++) {
    // xy = grid[y][start.getXpos()];
    // yx = grid[y][end.getXpos()];
    // if (xy.isVisible()) {
    // break;
    // }
    // path[0].add(xy);
    // if (!yx.isVisible() && xdirect(xy, yx, path[1])
    // && ydirect(yx, end, path[2])) {
    // return 3;
    // }
    // }
    //
    // path[0].clear();
    // for (int x = start.getXpos() - 1; x >= 0; x--) {
    // yx = grid[start.getYpos()][x];
    // xy = grid[end.getYpos()][x];
    // if (yx.isVisible()) {
    // break;
    // }
    // path[0].add(yx);
    // if (!xy.isVisible() && ydirect(yx, xy, path[1])
    // && xdirect(xy, end, path[2])) {
    // return 3;
    // }
    // }
    //
    // path[0].clear();
    // for (int x = start.getXpos() + 1; x < xBound; x++) {
    // yx = grid[start.getYpos()][x];
    // xy = grid[end.getYpos()][x];
    // if (yx.isVisible()) {
    // break;
    // }
    // path[0].add(yx);
    // if (!xy.isVisible() && ydirect(yx, xy, path[1])
    // && xdirect(xy, end, path[2])) {
    // return 3;
    // }
    // }
    //
    // return 0;
    // }

    // private class AnimateThread extends Thread {
    //
    // private LinkedList<Grid> v;
    //
    // public AnimateThread(LinkedList<Grid> temp) {
    //
    // v = temp;
    // }
    //
    // public void run() {
    // Grid prev = null;
    // for (int j = 0; j < v.size();) {
    // prev = (Grid) v.remove(0);
    // prev.setVisible(true);
    // v.add(prev);
    // j++;
    // try {
    // sleep(20L);
    // } catch (InterruptedException ire) {
    // }
    // }
    // Grid current = prev;
    // prev = (Grid) v.remove(0);
    // while (!v.isEmpty()) {
    // Grid o = (Grid) v.remove(0);
    // o.setVisible(false);
    // delCount++;
    // try {
    // sleep(20L);
    // } catch (InterruptedException ire) {
    // }
    // }
    // prev.setVisible(false);
    // current.setVisible(false);
    // current.setImage(Images.getInstance().getImage(9));
    // prev.setImage(Images.getInstance().getImage(9));
    // current.setBorder(1);
    // prev.setBorder(1);
    // // if (!findPair()) {
    // // refreshs();
    // // }
    // }
    //
    // }

    // private void shuffle(Grid array[], int count) {
    // if (wingame) {
    // return;
    // }
    // int number = 0;
    // do {
    // getSprites().setVisible(false);
    // for (int i = 0; i < count; i++) {
    // int j = (int) (Math.random() * (double) count);
    // int k = (int) (Math.random() * (double) count);
    // LImage temp = array[k].getImage();
    //
    // array[k].setImage(array[j].getImage());
    // array[j].setImage(temp);
    // }
    //
    // getSprites().setVisible(true);
    // number++;
    // if (number > 5) {
    // wingame = true;
    // break;
    // }
    // } while (!findPair());
    // }

}
